package com.lovo.sheyingback.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.sheyingback.bean.SystemDict;
import com.lovo.sheyingback.bean.Type;
import com.lovo.sheyingback.dao.TypeMapper;
import com.lovo.sheyingback.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;
    @Override
    public PageInfo<Type> listType(Integer current, Integer pageSize) {
        PageHelper.startPage(current,pageSize);
        List<Type> types = typeMapper.listType();
        return new PageInfo<>(types);
    }

    @Override
    public List<Type> listAllType() {
        return typeMapper.listType();
    }

    @Override
    public Type getById(Integer id) {
        return typeMapper.getById(id);
    }

    @Override
    public int insert(Type type) {
        type.setIndex(typeMapper.getMaxIndex());
        return typeMapper.insert(type);
    }

    @Override
    public int update(Type type) {
        return typeMapper.update(type);
    }

    @Override
    @Transactional
    public int updateIndex(Integer id, Integer val) {
        Type type = typeMapper.getById(id);
        type.setIndex(type.getIndex() +val);
        typeMapper.updateIndex(type.getIndex(),-val);
        typeMapper.update(type);
        return 1;
    }

}
